// address_book.cpp
// Implementation of the AddressBookEntry class
#include "address-book.h"

using namespace std;

// AddressBookEntry Constructor
AddressBookEntry::AddressBookEntry(	const Person& person,
								   const string& image_filename)
								   :_person(person),
								   _image_ptr()
{
	// dynamically allocate memory for the image
	if (image_filename != "") {
		// ignore (for now) what will happen if there is a memory allocation failure
		_image_ptr.reset(new Image(image_filename));
	}
}

AddressBookEntry::AddressBookEntry( const AddressBookEntry& rhs):
                  _person(rhs._person), _image_ptr()
{
    if (rhs.getImageName()!= "") {
		// ignore (for now) what will happen if there is a memory allocation failure
      _image_ptr.reset(new Image(*rhs._image_ptr));
	}
}

string AddressBookEntry::getImageName() const
{
	if (_image_ptr != 0)
		return _image_ptr->name();
	else return "No image!";
}

void AddressBookEntry::setImageName(string newname)
{
	if (_image_ptr != 0) _image_ptr->name(newname);
}

bool AddressBookEntry::operator==(const AddressBookEntry& rhs) const
{
    if( _person == rhs._person && _image_ptr == rhs._image_ptr )

        return true;
    else
        return false;
}

void AddressBookEntry::swapEntry( AddressBookEntry& rhs )
{
    std::swap( _person, rhs._person);

    _image_ptr.swap(rhs._image_ptr);

}

AddressBookEntry& AddressBookEntry::operator=( const AddressBookEntry& rhs)
{
    //temporary object
    AddressBookEntry temp(rhs);

    //swap temporary with *this object
    swapEntry(temp);

    //return the assigned object
    return *this;
}
